#!/usr/bin/env python
# -*- coding: utf-8 -*-

from django.http import HttpResponse, Http404
from django.core.serializers.json import DjangoJSONEncoder
from django.contrib.auth import authenticate, login
from django.views.decorators.csrf import csrf_exempt
#from models import*
import json


def get_flaginfo(logged, username, password):
    if logged:
        flag = {'flag':(0, 1, 2)}
    else:
        flag = {'flag':(-1,)}
    flaginfo = {
        'QRinfo':flag,
        'user':{
            'username':username,
            'password':password,
        }
    }
    return json.dumps(flaginfo, ensure_ascii=False, cls=DjangoJSONEncoder)


@csrf_exempt
def phone_login(request):
    if request.method=="POST":
        user = authenticate(username=request.POST['username'], password=request.POST['password'])
        if user is not None:
            login(request, user)
            logged = True
        else:
            logged = False
        return HttpResponse(get_flaginfo(logged, request.POST['username'], request.POST['password']))
    else:
        return Http404
